package com.imooc.service.Impl;

import com.imooc.dao.ProductCategoryMapper;
import com.imooc.dto.ProductCategoryExecution;
import com.imooc.entity.ProductCategory;
import com.imooc.enums.ProductCategoryStateEnum;
import com.imooc.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author
 * @create 2018-08-02 20:24
 **/
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {


    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    /**
     * 批量增加商品类别
     *
     * @param productCategoryList
     * @return
     */
    @Override
    @Transactional
    public ProductCategoryExecution batchAddProductCategory(List<ProductCategory> productCategoryList) {

        //判断产品列表是否为空
        if (productCategoryList != null && productCategoryList.size() > 0) {
            int effectNum = productCategoryMapper.batchInsertProductCategory(productCategoryList);
            if (effectNum <= 0) {
                return new ProductCategoryExecution(ProductCategoryStateEnum.INNER_ERROR);
            } else {
                return new ProductCategoryExecution(ProductCategoryStateEnum.SUCCESS);
            }
        } else {
            return new ProductCategoryExecution(ProductCategoryStateEnum.EMPTY_LIST);
        }
    }

    /**
     * 根据shopId获取对应的商品分类信息
     *
     * @param shopId
     * @return
     */
    @Override
    public List<ProductCategory> getProductCategoryList(int shopId) {
        return productCategoryMapper.queryByShopId(shopId);
    }

    /**
     * 通过分类id和商品id分类删除商品
     *
     * @param productCategoryId
     * @param shopId
     * @return
     * @throws RuntimeException
     */
    @Override
    @Transactional
    public ProductCategoryExecution deleteProductCategory(int productCategoryId, int shopId) throws RuntimeException {

        try {
            int effectNum = productCategoryMapper.deleteProductCategory(productCategoryId, shopId);
            if (effectNum <= 0) {
                throw new RuntimeException("店铺类别删除失败");
            }else {
                return new ProductCategoryExecution(ProductCategoryStateEnum.SUCCESS);
            }
        } catch (Exception e) {
            throw new RuntimeException("deleteProductCategory error:" + e.getMessage());
        }

    }
}
